<template>
    <div>
     <h1>机构添加</h1>
     <table class="table table-bordered">
        <tbody>
            <tr>
                <td align="right">机构名称</td>
                <td>
                    <input type="text" v-model="info.PensionName">
                </td>
            </tr>
            <tr>
                <td align="right">机构封面</td>
                <td>
                    <input type="file" @change="img">
                    <img :src="info.Photo" width="100" height="100" v-if="info.Photo!=''">
                </td>
            </tr>
            <tr>
                <td align="right">特色服务</td>
                <td>
                    <select v-model="info.ServeName">
                        <option value="">请选择</option>
                        <option v-for="item in xlk" :value="item.ServeName">{{item.ServeName}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">机构地址</td>
                <td>
                    <input type="text" v-model="info.Addres">
                </td>
            </tr>
            <tr>
                <td align="right">联系电话</td>
                <td>
                    <input type="text" maxlength="11" v-model="info.Phone">
                </td>
            </tr>
            <tr>
                <td align="right">状态</td>
                <td>
                    <input type="radio" v-model="info.State"  name="state" :value="1">待发布
                    <input type="radio" v-model="info.State"  name="state" :value="0">已发布
                </td>
            </tr>
            <tr>
                <td align="right"></td>
                <td>
                    <input type="button" value="添加" class="btn btn-primary" @click="ok">
                </td>
            </tr>
        </tbody>
     </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted}from 'vue';
import axios from 'axios';
import moment from 'moment';

import {useRouter,useRoute} from 'vue-router';
const router=useRouter();
const route=useRoute();

import { useStore } from '@/stores/user';
const a=useStore();

//定义变量
let info:any=reactive({
  "PensionName": "",
  "ServeName": "",
  "Photo": "",
  "Addres": "",
  "Phone": "",
  "State": 1,
})
//===========服务下拉框==========
let xlk:any=ref([]);

onMounted(()=>{
    serve();
})

const serve=()=>{
    axios({
        url:'/api/Pension/Getservelist',
        method:'get',
        params:xlk,
    })
    .then(res=>{
        console.log(res);
        xlk.value=res.data;
    })
}
//===============图片上传================
const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append("file",obj);

    axios({
        url:'/api/Img/Img',
        method:'post',
        data:f,
    })
    .then(res=>{
        console.log(res);
        if(res.data=='不是图片'){
            alert('图片格式不正确');
            return;
        }
        if(res.data=='图片过大'){
            alert('图片格式大于1M');
            return;
        }
        info.Photo=res.data;
    })
}
//=================添加===================
const ok=()=>{
    axios({
        url:'/api/Pension/AddPension',
        method:'post',
        data:info,
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('添加成功');
            router.push('/pensionlist')
        }
        else if(res.data==-1){
            alert('机构名称重复');
            return;
        }
        else{
            alert('添加失败');
            return;
        }
    })
}

</script>

<style scoped>

</style>